package cn.edu.sdu.databasedocument.util;

import cn.edu.sdu.databasedocument.model.DatabaseDocumentModel;
import cn.smallbun.screw.core.Configuration;
import cn.smallbun.screw.core.engine.EngineConfig;
import cn.smallbun.screw.core.engine.EngineFileType;
import cn.smallbun.screw.core.engine.EngineTemplateType;
import cn.smallbun.screw.core.execute.DocumentationExecute;
import cn.smallbun.screw.core.process.ProcessConfig;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;

import javax.sql.DataSource;

public class DataSourceDocumentGeneration {

    public static void create(DatabaseDocumentModel model) {

        //数据源
        HikariConfig hikariConfig = new HikariConfig();
        hikariConfig.setDriverClassName("com.mysql.cj.jdbc.Driver");
        hikariConfig.setJdbcUrl("jdbc:mysql://" + model.getIp() + ":" + model.getPort() + "/" + model.getDatabase());
        hikariConfig.setUsername(model.getUsername());
        hikariConfig.setPassword(model.getPassword());
        hikariConfig.addDataSourceProperty("useInformationSchema", "true");
        hikariConfig.addDataSourceProperty("characterEncoding","UTF-8");
        hikariConfig.addDataSourceProperty("useUnicode","true");
        hikariConfig.setMinimumIdle(2);
        hikariConfig.setMaximumPoolSize(5);
        DataSource dataSource = new HikariDataSource(hikariConfig);
        //生成配置
        EngineConfig engineConfig = EngineConfig.builder()
                //生成文件路径
                .fileOutputDir(model.getFileOutputDir())
                //文件类型
                .fileType(EngineFileType.valueOf(model.getFileType()))
                //生成模板实现
                .produceType(EngineTemplateType.freemarker)
                //自定义文件名称
                .fileName(model.getFileName()).build();

        ProcessConfig processConfig = ProcessConfig.builder()
                //指定生成逻辑、当存在指定表、指定表前缀、指定表后缀时，将生成指定表，其余表不生成、并跳过忽略表配置
                //根据名称指定表生成
                .designatedTableName(model.getDesignatedTableName())
                //根据表前缀生成
                .designatedTablePrefix(model.getDesignatedTablePrefix())
                //根据表后缀生成
                .designatedTableSuffix(model.getDesignatedTableSuffix())
                //忽略表名
                .ignoreTableName(model.getIgnoreTableName())
                //忽略表前缀
                .ignoreTablePrefix(model.getIgnorePrefix())
                //忽略表后缀
                .ignoreTableSuffix(model.getIgnoreSuffix()).build();
        //配置
        Configuration config = Configuration.builder()
                //版本
                .version(model.getVersion())
                //描述
                .description(model.getDescription())
                //数据源
                .dataSource(dataSource)
                //生成配置
                .engineConfig(engineConfig)
                //生成配置
                .produceConfig(processConfig)
                .build();
        //执行生成
        new DocumentationExecute(config).execute();
    }

    public static void main(String[] args) {
        DatabaseDocumentModel model = new DatabaseDocumentModel();
        model.setIp("219.231.141.52");
        model.setPort("3306");
        model.setDatabase("weather_forecast");
        model.setUsername("root");
        model.setPassword("Sddx2023@");
        model.setFileOutputDir("C:\\Users\\13167\\Desktop\\数据库");
        model.setFileName("数据库文档");
        model.setVersion("1.0.0");
        model.setDescription("数据库文档");
        model.setFileType("WORD");
//        model.setOpenOutputDir(true);
        create(model);
    }
}
